package com.recruit.wechat.utils.dictutils;

import com.recruit.wechat.bean.WechatIndustry;
import com.recruit.wechat.bean.WechatPosition;
import com.recruit.wechat.service.WechatIndustryService;
import com.recruit.wechat.service.WechatPositionService;
import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 职位下拉
 * @author LF
 * @date 2020/9/2 - 11:29
 *
 */
@Component
public class IndustryDirective implements TemplateDirectiveModel {
    @Autowired
    WechatIndustryService wechatIndustryService;

    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException, IOException {
        DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
        List<WechatIndustry> list = wechatIndustryService.list();
        if(map.containsKey("value") && map.get("value") != null) {
            String value = map.get("value").toString();
            WechatIndustry wechatIndustry = null;
            for (WechatIndustry dict1 : list) {
                String s = dict1.getIndustryId().toString();
                if(value.equals(s)){
                    wechatIndustry = dict1;
                }
            }
            environment.setVariable("industry", builder.build().wrap(wechatIndustry));
        }else {
            environment.setVariable("industryList", builder.build().wrap(list));
        }

        if(templateDirectiveBody!=null){
            templateDirectiveBody.render(environment.getOut());
        }
    }
}
